'use strict';

const autoprefixer = require('autoprefixer');
const path = require('path');
const webpack = require('webpack');
const HtmlWebpackPlugin = require('html-webpack-plugin');
const ExtractTextPlugin = require('extract-text-webpack-plugin');
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const UglifyJsPlugin = require('uglifyjs-webpack-plugin');
const OptimizeCSSAssetsPlugin = require("optimize-css-assets-webpack-plugin");
const CaseSensitivePathsPlugin = require('case-sensitive-paths-webpack-plugin');
const InterpolateHtmlPlugin = require('react-dev-utils/InterpolateHtmlPlugin');
const WatchMissingNodeModulesPlugin = require('react-dev-utils/WatchMissingNodeModulesPlugin');
const eslintFormatter = require('react-dev-utils/eslintFormatter');
const ModuleScopePlugin = require('react-dev-utils/ModuleScopePlugin');
// const getClientEnvironment = require('./env');
const paths = require('./paths');
const glob = require('glob');

// Webpack uses `publicPath` to determine where the app is being served from.
// In development, we always serve from the root. This makes config easier.
const publicPath = '/';
// `publicUrl` is just like `publicPath`, but we will provide it to our app
// as %PUBLIC_URL% in `index.html` and `process.env.PUBLIC_URL` in JavaScript.
// Omit trailing slash as %PUBLIC_PATH%/xyz looks better than %PUBLIC_PATH%xyz.
const publicUrl = '';
// Get environment variables to inject into our app.
// const env = getClientEnvironment(publicUrl);

// This is the development configuration.
// It is focused on developer experience and fast rebuilds.
// The production configuration is different and lives in a separate file.

// List of vendor libraries to include in vendor js bundle
// const vendorLibs = require('./vendorLibs');
import {readFile} from './rf';
import { argv } from 'yargs';

import config from '../config.json';
import config_cdn from '../config_cdn.json';

let PUBLIC_PATH=config.STATIC_PREFIX;
let minimizerArray = [];
if(argv._.length){
    argv._[0] == 'cdn';
    PUBLIC_PATH=config_cdn.STATIC_PREFIX;
    minimizerArray = [
        new UglifyJsPlugin({
            uglifyOptions: {
                warnings: false,
                parse: {},
                compress: {
                    warnings: false,
                    drop_debugger: true,
                    drop_console: true
                },
                // mangle: true, // Note `mangle.properties` is `false` by default.
                // output: null,
                // toplevel: false,
                // nameCache: null,
                ie8: false,
                // keep_fnames: false
            }
        }),
        new OptimizeCSSAssetsPlugin({
        })
    ]
}
console.error(PUBLIC_PATH);
var webpackConfig = {
    mode : 'production',
    entry: readFile('ProdConfigs'),
    output: {
        path: path.join(__dirname, '..', '/server/public/masterBak'), // Note: Physical files are only output by the production build task `npm run build`.
        filename: '[name].js',//'/[name].[chunkhash].js',
        chunkFilename: "[chunkhash].js",
        publicPath: `${PUBLIC_PATH}/master/`
    },
    optimization: {
        splitChunks: {
            cacheGroups: {
                commons: {
                    name: "common",
                    filename: "common.js",
                    test: /\.js$/,
                    chunks: "initial",
                    minChunks: 10
                }
                // styles: {
                //     name: 'styles',
                //     test: /\.scss/,
                //     chunks: 'all',
                //     enforce: true
                // }
            }
        },
        minimizer: minimizerArray
        // minimize : true,
    },
    module: { //我写一个module
        rules:[
            {
                test: /\.less$/,
                use: [{
                    loader: 'style-loader',
                }, {
                    loader: 'css-loader', // translates CSS into CommonJS
                }, {
                    loader: 'less-loader', // compiles Less to CSS
                    options: {
                        modifyVars: {
                            'font-family' : '"Microsoft YaHei", "Helvetica Neue", Helvetica Neue, Helvetica, Arial, sans-serif',
                            'primary-color': '#df5064',                        // 全局主色
                            'link-color': '#df5064',                            // 链接色
                            'success-color': '#52c41a',                        // 成功色
                            'warning-color': '#faad14',                       // 警告色
                            'error-color': '#f5222d',                          // 错误色
                            'font-size-base': '14px',                           // 主字号
                            'heading-color': 'rgba(0, 0, 0, .85)',             // 标题色
                            'text-color': 'rgba(0, 0, 0, .65)',                 // 主文本色
                            'text-color-secondary': 'rgba(0, 0, 0, .45)',     // 次文本色
                            'disabled-color': 'rgba(0, 0, 0, .25)',            // 失效色
                            'border-radius-base':'3px',                        // 组件/浮层圆角
                            'border-color-base': '#d9d9d9',                     // 边框色
                            'box-shadow-base': '0 2px 8px rgba(0, 0, 0, .15)'  // 浮层阴影
                        },
                        javascriptEnabled: true,
                    }
                }]
            },
            { // 这里用rules而不是loaders
                test:  /\.jsx?$/, // 匹配所有js文件
                exclude: /node_modules/, // 排除掉node_modules这个文件夹的js文件
                loader: 'babel-loader'
            },
            {
                test: /\.(css|scss)$/,
                use : [
                    MiniCssExtractPlugin.loader,
                    'css-loader', 'sass-loader'
                ]
                // use: ExtractTextPlugin.extract({
                //     fallback: 'style-loader',
                //     use: ['css-loader', 'sass-loader']
                // })
            },
            {
                test:/\.(jpe?g|png|gif|woff|svg|eot|ttf)\??.*$/,
                use:[{
                    loader:'url-loader',
                    options:{ // 这里的options选项参数可以定义多大的图片转换为base64
                        limit:50000, // 表示小于50kb的图片转为base64,大于50kb的是路径
                        outputPath:'images' //定义输出的图片文件夹
                    }
                }]
            }, {
                test: /\.json$/,
                loader: 'json-loader'
            }/*,
            {
                test: /\.(scss|css|pdf)$/,
                loader: 'ignore-loader',
            }*/
        ]
    },
    plugins: [
        new MiniCssExtractPlugin({
            filename: "css/[name].css",
            chunkFilename : "css/[name]_[chunkhash].css"
        })
        // new ExtractTextPlugin("css/[name].css")
        //if you want to pass in options, you can do so:
        //new ExtractTextPlugin({
        //  filename: 'style.css'
        //})
    ]
};
const nodeExternals = require('webpack-node-externals');
module.exports = [webpackConfig,Object.assign({}, webpackConfig, {
    entry: {
        serverRender: ['./src/serverRender.js']
    },
    output: Object.assign({}, webpackConfig.output, {
        filename: '[name].js',
        libraryTarget: 'commonjs2'
    }),
    // plugins: [],
    target: 'node',
    externals: [nodeExternals()]
})];